package com.glasses.business.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.facebook.drawee.view.SimpleDraweeView;
import com.glasses.business.R;
import com.glasses.business.base.MyBaseAdapter;
import com.glasses.business.entity.CheckMenu;
import com.glasses.business.entity.LightList;
import com.glasses.business.event.BussEvent;
import com.glasses.business.http.NetUtils;
import com.glasses.business.utils.TimeUtils;
import com.glasses.business.utils.Tools;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.Result;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/6
 * Version：1.0
 */

public class LightOrderAdapter extends MyBaseAdapter<LightList, LightOrderAdapter.ViewHolder> {


    public LightOrderAdapter(Activity context, List data) {
        super(context, data);
    }

    @Override
    protected ViewHolder getViewHolder() {
        return new ViewHolder(mInflater.inflate(R.layout.item_light_order, null));
    }

    @Override
    protected void onBindViewHolder_(ViewHolder holder, int position) {
        if (data != null && data.size() > 0) {
            final LightList lightList = data.get(position);
            final String content = lightList.getContent();
            switch (lightList.getType()) {
                case 0:
                    holder.lightTableLL.setVisibility(View.VISIBLE);
                    holder.lightImgLL.setVisibility(View.GONE);
                    CheckMenu checkMenu = JSON.parseObject(content, CheckMenu.class);
                    holder.lightLeftAXL.setText(checkMenu.getLeftAXL());
                    holder.lightLeftCYL.setText(checkMenu.getLeftCYL());
                    holder.lightLeftSPH.setText(checkMenu.getLeftSPH());
                    holder.lightRightAXL.setText(checkMenu.getRightAXL());
                    holder.lightRightCYL.setText(checkMenu.getRightCYL());
                    holder.lightRightSPH.setText(checkMenu.getRightSPH());
                    holder.lightPD.setText(checkMenu.getEyesPD());
                    holder.lightOrderTime.setText(lightList.getUpdateTime());
                    holder.lightDelImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            new AlertDialog.Builder(context).setMessage("确认删除验光单？")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            dialogInterface.dismiss();
                                            delLightOrder(lightList);
                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            }).create().show();
                        }
                    });
                    return;
                case 1:
                    holder.lightTableLL.setVisibility(View.GONE);
                    holder.lightImgLL.setVisibility(View.VISIBLE);
                    holder.lightOrderTime_.setText(lightList.getUpdateTime());
                    String imgPath = null;
                    try {
                        imgPath = new JSONObject(content).getString("imgPath");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    holder.lightImg.setImageURI(imgPath);
                    holder.lightDelImg_.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            new AlertDialog.Builder(context).setMessage("确认删除验光单？")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            dialogInterface.dismiss();
                                            delLightOrder(lightList);
                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            }).create().show();
                        }
                    });
                    return;
            }
        }
    }

    private void delLightOrder(LightList lightList) {
        Tools.showDialog(context);
        NetUtils.getInstance().delCheckMenu(lightList.getId(), new NetCallBack() {
            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                EventBus.getDefault().post(new BussEvent(BussEvent.Save_Light_Order));
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
            }
        }, null);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.lightTableLL)
        LinearLayout lightTableLL;
        @InjectView(R.id.lightCheckImg)
        ImageView lightCheckImg;
        @InjectView(R.id.lightCheckLL)
        LinearLayout lightCheckLL;
        @InjectView(R.id.lightOrderTime)
        TextView lightOrderTime;
        @InjectView(R.id.lightLeftSPH)
        TextView lightLeftSPH;
        @InjectView(R.id.lightLeftCYL)
        TextView lightLeftCYL;
        @InjectView(R.id.lightLeftAXL)
        TextView lightLeftAXL;
        @InjectView(R.id.lightRightSPH)
        TextView lightRightSPH;
        @InjectView(R.id.lightRightCYL)
        TextView lightRightCYL;
        @InjectView(R.id.lightRightAXL)
        TextView lightRightAXL;
        @InjectView(R.id.lightPD)
        TextView lightPD;
        @InjectView(R.id.lightDelImg)
        ImageView lightDelImg;

        @InjectView(R.id.lightImgLL)
        LinearLayout lightImgLL;
        @InjectView(R.id.lightCheckImg_)
        ImageView lightCheckImg_;
        @InjectView(R.id.lightCheckLL_)
        LinearLayout lightCheckLL_;
        @InjectView(R.id.lightOrderTime_)
        TextView lightOrderTime_;
        @InjectView(R.id.lightImg)
        SimpleDraweeView lightImg;
        @InjectView(R.id.lightDelImg_)
        ImageView lightDelImg_;

        ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
